package control

import (
	"mvip-route/service/damRed"
	"mvip-route/service/damRed/impl"
	"mvip-route/service/model"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

type DamDomainIpContr struct {
	DamDomainService damRed.DamDomainService
}

func (ctrl *DamDomainIpContr) CreateDamDomain(ctx *gin.Context) {
	service := impl.DamDomainIpService{}
	var newsDamDomain model.DamDomainIpModel
	if err := ctx.ShouldBindJSON(&newsDamDomain); err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"data": "解析错误"})
	}
	newDamDomain, err := service.CreateDamDomain(newsDamDomain)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
	}
	ctx.JSON(http.StatusOK, gin.H{"data": newDamDomain, "code": http.StatusOK})
}

func (ctrl *DamDomainIpContr) GetDamDomain(ctx *gin.Context) {
	service := impl.DamDomainIpService{}
	damDomain, err := service.GetDamDomain()
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
	}
	ctx.JSON(http.StatusOK, damDomain)
}
func (ctrl *DamDomainIpContr) DeleteDamDomain(ctx *gin.Context) {
	id, _ := strconv.Atoi(ctx.Param("id"))
	service := impl.DamDomainIpService{}
	if err := service.DeleteDamDomain(id); err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{"data": "delete success", "code": http.StatusOK})
}
